package com.qf.nio.serversocket;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @Description:
 * @Author: xbb
 * @Date:2020/4/20
 */
public class ClientTest {

    public static void main(String[] args) throws IOException {
        //1.SocketChannel
        SocketChannel socketChannel = SocketChannel.open();

        //2.连接服务器
        socketChannel.connect(new InetSocketAddress(8080));
        System.out.println("客户端连接成功");

        //3.写入数据
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024*10);
        byteBuffer.put("hello".getBytes("utf-8"));
        byteBuffer.flip();  //改为可读模式
        socketChannel.write(byteBuffer);
        System.out.println("写入模式");

        //4.读取服务端相应的数据
        byteBuffer.clear();
        socketChannel.read(byteBuffer);
        System.out.println("客户端读取内容："+new String(byteBuffer.array()));
    }

}
